package edu.zjut.hjg.saas.kwmp.web.controller.home;

import static edu.zjut.hjg.saas.kwmp.web.support.KwmpConstants.PAGE_SIZE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.service.NewsService;
import edu.zjut.hjg.saas.common.annotation.RequestAttribute;

@Controller
@RequestMapping("/home/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @RequestMapping("list.htm")
    public void list(@RequestAttribute Kindergarten app,
                     @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                     Model model) {
        if (page < 1) {
            page = 1;
        }
        model.addAttribute("paginated", newsService.listByAppId(app.getId(), PAGE_SIZE, page));
    }

    @RequestMapping("detail.htm")
    public void detail(@RequestAttribute Kindergarten app, int id, Model model) {
        model.addAttribute("news", newsService.findByIdAndAppId(id, app.getId()));
    }
}
